from django.test import TestCase, Client
from django.http import JsonResponse
import json
from unittest.mock import patch, MagicMock
import os

from django.urls import reverse


#模拟虚拟发送
class GenerateGuidanceTestCase1(TestCase):
    def setUp(self):
        self.client = Client()
        self.url = reverse('generate_guidance')

    @patch('requests.post')
    def test_successful_form_request(self, mock_post):
        # 设置模拟的API响应
        mock_response = mock_post.return_value
        mock_response.status_code = 200
        mock_response.json.return_value = {
            "output": "这是一段生成的引导文本"
        }

        form_data = {
            "event": "人工智能发展",
            "guideline": "客观看待技术利弊",
            "misconception": "AI会完全取代人类工作"
        }

        response = self.client.post(self.url, form_data)

        # 验证响应
        self.assertEqual(response.status_code, 200)
        response_data = json.loads(response.content)

        # 检查响应数据
        self.assertEqual(response_data['success'], True)
        self.assertIn('guidance_text', response_data)
        self.assertIn('response_time', response_data)

    @patch('requests.post')
    def test_missing_parameter(self, mock_post):
        """测试缺少参数的情况"""
        form_data = {
            "event": "人工智能发展",
            "guideline": "客观看待技术利弊",
            # 缺少misconception
        }

        response = self.client.post(self.url, form_data)

        # 验证返回400错误
        self.assertEqual(response.status_code, 400)
        response_data = json.loads(response.content)
        self.assertIn('error', response_data)
        self.assertIn('Missing required parameters', response_data['error'])

    @patch('requests.post')
    def test_api_error(self, mock_post):
        # 设置模拟的API错误响应
        mock_response = mock_post.return_value
        mock_response.status_code = 500
        mock_response.text = "Ollama API内部错误"

        form_data = {
            "event": "测试",
            "guideline": "测试引导",
            "misconception": "测试误解"
        }

        response = self.client.post(self.url, form_data)

        # 验证返回500错误
        self.assertEqual(response.status_code, 500)
        response_data = json.loads(response.content)
        self.assertFalse(response_data['success'])
        self.assertIn('error', response_data)
        self.assertIn('模型API调用失败', response_data['error'])


# #模拟实际发送
# class GenerateGuidanceTestCase2(TestCase):
#     def setUp(self):
#         self.client = Client()
#         # 设置环境变量
#         os.environ['OLLAMA_API_URL'] = 'http://localhost:8889/generate-guidance/invoke'
#
#     @patch('intelligence_interaction.views.requests.post')  # 替换为你的视图所在的模块
#     def test_generate_guidance_success(self, mock_post):
#         # 模拟外部API的成功响应
#         mock_response = MagicMock()
#         mock_response.status_code = 200
#         mock_response.json.return_value = {
#             "output": "新能源汽车引发火灾的原因是多方面的...",
#             "metadata": {
#                 "run_id": "5aeeb10b-2cf0-40d4-8659-6509e6dae2ba",
#                 "feedback_tokens": []
#             }
#         }
#         mock_post.return_value = mock_response
#
#         # 准备测试数据
#         test_data = {
#             "input": {
#                 "event": "新能源汽车引发火灾",
#                 "guideline": "引导用户理解火灾可能涉及多个因素，不应武断否定新能源车的发展",
#                 "misconception": "新能源汽车经常自燃，说明这项技术根本不成熟，应该全部禁售"
#             }
#         }
#
#         # 发送POST请求
#         response = self.client.post(
#             '/your-url-path/',  # 替换为实际的URL路径
#             json.dumps(test_data),
#             content_type='application/json'
#         )
#
#         # 验证响应状态码
#         self.assertEqual(response.status_code, 200)
#
#         # 解析响应数据
#         response_data = json.loads(response.content)
#
#         # 验证响应数据结构和内容
#         self.assertTrue(response_data['success'])
#         self.assertIn('response_time', response_data)
#         self.assertIn('guidance_text', response_data)
#         self.assertEqual(response_data['guidance_text'], "新能源汽车引发火灾的原因是多方面的...")
#
#     @patch('intelligence_interaction.views.requests.post')  # 替换为你的视图所在的模块
#     def test_generate_guidance_api_failure(self, mock_post):
#         # 模拟外部API的失败响应
#         mock_response = MagicMock()
#         mock_response.status_code = 500
#         mock_response.text = "Internal Server Error"
#         mock_post.return_value = mock_response
#
#         test_data = {
#             "input": {
#                 "event": "新能源汽车引发火灾",
#                 "guideline": "引导用户理解火灾可能涉及多个因素，不应武断否定新能源车的发展",
#                 "misconception": "新能源汽车经常自燃，说明这项技术根本不成熟，应该全部禁售"
#             }
#         }
#
#         response = self.client.post(
#             '/generate_guidance/',  # 替换为实际的URL路径
#             json.dumps(test_data),
#             content_type='application/json'
#         )
#
#         self.assertEqual(response.status_code, 500)
#         response_data = json.loads(response.content)
#         self.assertFalse(response_data['success'])
#         self.assertIn('error', response_data)
#         self.assertIn('details', response_data)
#
#     def test_generate_guidance_missing_parameters(self):
#         # 测试缺少必要参数的情况
#         test_data = {
#             "input": {
#                 "event": "新能源汽车引发火灾",
#                 # 缺少guideline和misconception
#             }
#         }
#
#         response = self.client.post(
#             '/generate_guidance/',  # 替换为实际的URL路径
#             json.dumps(test_data),
#             content_type='application/json'
#         )
#
#         self.assertEqual(response.status_code, 400)
#         response_data = json.loads(response.content)
#         self.assertIn('error', response_data)
#         self.assertIn('Missing required parameters', response_data['error'])